﻿/*
 * Copyright (c) Seagull Consulting, Inc. All rights reserved. See License.txt in the project root for license information.
 */
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SeagullConsulting.MvcAjaxControls.Data.Models;
using SeagullConsulting.MvcAjaxControls.Data.Repository;

namespace SeagullConsulting.MvcAjaxControls.Web.UI.Controllers
{
    public class PhoneController : Controller
    {

        private PhoneRepository repository;

        public PhoneController()
        {
            repository = new PhoneRepository();
            ViewData["controller"] = typeof(AjaxGridController).Name;
        }

        //
        // GET: /Phone/ or /Phone/{id}

        public ActionResult Index(int? id)
        {
            return View("Index");
        }

        #region ListPage
        //
        // GET: /Phone/ListPage or /Phone/ListPage/{id}

        public ActionResult ListPage(int? id)
        {
            return View("ListPage");
        }

        public JsonResult ListPhone(int? id, int pageNbr, int pageSize)
        {
            PagerParms parms;
            IEnumerable<Phone> phones;

            try
            {
                if (id.HasValue)
                {
                    if (pageSize > 0)
                    {
                        //Paging is being used
                        parms = new PagerParms() { PageSize = (int)pageSize };
                        if (pageNbr > 1)
                            parms.PageNbr = (int)pageNbr - 1;
                        else
                            parms.PageNbr = 0;
                        phones = repository.List((int)id, parms);
                    }
                    else
                    {
                        //Paging not being used.
                        phones = repository.List((int) id);
                    }
                    return Json(phones, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    if (pageSize > 0)
                    {
                        //Paging is being used
                        parms = new PagerParms() { PageSize = (int)pageSize };
                        if (pageNbr > 1)
                            parms.PageNbr = (int)pageNbr - 1;
                        else
                            parms.PageNbr = 0;
                        phones = repository.List(parms);
                    }
                    else
                    {
                        //Paging not being used.
                        phones = repository.List();
                    }
                    return Json(phones, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception: " + ex.Message);
                throw (Exception)Activator.CreateInstance(ex.GetType(), ex.Message + ex.StackTrace);
            }
        }

        public JsonResult ListPhoneCount(int? id)
        {
            try
            {
                if (id.HasValue)
                    return Json(repository.ListCount((int)id), JsonRequestBehavior.AllowGet);
                else
                    return Json(repository.ListCount(), JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception: " + ex.Message);
                throw (Exception)Activator.CreateInstance(ex.GetType(), ex.Message + ex.StackTrace);
            }
        }
        #endregion
    }
}
